<?php
?>

<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group col-lg-6">
                <a class="btn btn-default " type="button" data-toggle="modal" href="#addModal"><i
                        class="fa fa-plus"></i> 充值</a>
            </div>
            <div class="pull-right">
                <div class="form-group">
                    <?php
                    if ($current_institution_code == '0') {
                        ?>
                        <?= \app\widgets\InstitutionHierarchyAutocompleteWidget::widget(['id' => 'searchInstitution', 'defaultText' => $current_institution_name, 'currentInstitutionCode' => $current_institution_code]) ?>
                        <?php
                    } else {
                        ?>
                        <?= \app\widgets\InstitutionChildrenAutocompleteWidget::widget(['id' => 'searchInstitution', 'defaultText' => $current_institution_name, 'currentInstitutionCode' => $current_institution_code]) ?>
                        <?php
                    }
                    ?>
                </div>
                <div class="form-group">
                    <select class="form-control" id="searchRechargeStatus">
                        <option value="0" <?= $current_credit_status == '0' ? 'selected' : '' ?>>----全部状态----</option>
                        <option value="1" <?= $current_credit_status == '1' ? 'selected' : '' ?>>已提交申请</option>
                        <option value="11" <?= $current_credit_status == '11' ? 'selected' : '' ?>>审核成功</option>
                        <option value="12" <?= $current_credit_status == '12' ? 'selected' : '' ?>>审核失败</option>
                        <option value="20" <?= $current_credit_status == '20' ? 'selected' : '' ?>>充值成功</option>
                        <option value="21" <?= $current_credit_status == '21' ? 'selected' : '' ?>>充值失败</option>
                        <option value="22" <?= $current_credit_status == '22' ? 'selected' : '' ?>>充值取消</option>
                    </select>
                </div>
                <input type="text" class="form-control dpd1" id="startTime" name="from" placeholder="开始时间"
                       value="<?= $startTime ?>">
                <span class="form-control">To</span>
                <input type="text" class="form-control dpd2" id="endTime" name="to" placeholder="结束时间"
                       value="<?= $endTime ?>">

                <div class="btn-group">
                    <button type="button" class="btn btn-primary" id="btnSearch">
                        <i class="fa fa-search"></i> 搜索
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="panel">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th>机构</th>
                    <th>充值金额</th>
                    <th>充值方式</th>
                    <th>付款单位</th>
                    <th>付款时间</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                foreach ($items as $item) {
                    ?>
                    <tr>
                        <td><?= $item['institution_name'] ?></td>
                        <td class="nowrap"><?= $item['amount'] ?></td>
                        <td class="nowrap">
                            <?php
                            switch ($item['mode']) {
                                case 1:
                                    echo '转账';
                                    break;
                                case 2:
                                    echo '汇款';
                                    break;
                                case 3:
                                    echo '支票';
                                    break;
                                case 4:
                                    echo '现金';
                                    break;
                            }
                            ?>
                        </td>
                        <td class="nowrap"><?= $item['payment_name'] ?></td>
                        <td class="nowrap"><?= $item['create_datetime'] ?></td>
                        <td class="nowrap">
                            <?php
                            switch ($item['status']) {
                                case 1:
                                    echo "<label class='label label-warning'>{$item['status_name']}</label>";
                                    break;
                                case 11:
                                    echo "<label class='label label-success'>{$item['status_name']}</label>";
                                    break;
                                case 12:
                                    echo "<label class='label label-danger'>{$item['status_name']}</label>";
                                    break;
                                default:
                                    echo $item['status_name'];
                                    break;
                            }
                            ?>
                        </td>
                        <td>
                            <div class="btn-group">
                                <a class="btn btn-link" data-toggle="modal" data-recharge-id="<?= $item['id'] ?>"
                                   href="#detailModal">详情</a>
                            </div>
                        </td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>

<div aria-hidden="true" role="dialog" tabindex="-1" id="detailModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">充值详情</h4>
            </div>
            <div class="modal-body" id="detail_content">
                正在加载数据....
            </div>
        </div>
    </div>
</div>

<div aria-hidden="true" role="dialog" tabindex="-1" id="addModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">机构充值</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="modaledit_submit_form" method="post" enctype="multipart/form-data"
                      action="add-recharge-handler" target="upload_exec_target">
                    <div class="form-horizontal">
                        <div class="form-group">
                            <label class="col-lg-3 col-sm-3 control-label">机构:</label>
                            <div class="col-lg-9">
                                <?php
                                if ($current_institution_code == '0') {
                                    ?>
                                    <?= \app\widgets\InstitutionHierarchyAutocompleteWidget::widget(['id' => 'inputInstitution', 'defaultText' => $current_institution_name, 'currentInstitutionCode' => $current_institution_code]) ?>
                                    <?php
                                } else {
                                    ?>
                                    <?= \app\widgets\InstitutionChildrenAutocompleteWidget::widget(['id' => 'inputInstitution', 'defaultText' => $current_institution_name, 'currentInstitutionCode' => $current_institution_code]) ?>
                                    <?php
                                }
                                ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-3 col-sm-3 control-label">充值金额:</label>
                            <div class="col-lg-9">
                                <div class="input-group">
                                    <span class="input-group-addon">￥</span>
                                    <input type="text" class="form-control" id="inputRechargeAmount"
                                           name="inputRechargeAmount">
                                    <span class="input-group-addon">.00</span>
                                </div>
                            </div>
                            :
                        </div>
                        <div class="form-group">
                            <label class="col-lg-3 col-sm-3 control-label">充值方式:</label>
                            <div class="col-lg-9 icheck form-inline">
                                <div class="square-blue">
                                    <div class="radio">
                                        <input type="radio" checked name="recharge_mode" value="1">
                                        <label>转账 </label>
                                    </div>
                                </div>
                                <div class="square-blue">
                                    <div class="radio">
                                        <input type="radio" name="recharge_mode" value="2">
                                        <label>汇款 </label>
                                    </div>
                                </div>
                                <div class="square-blue">
                                    <div class="radio">
                                        <input type="radio" name="recharge_mode" value="3">
                                        <label>支票 </label>
                                    </div>
                                </div>
                                <div class="square-blue">
                                    <div class="radio">
                                        <input type="radio" name="recharge_mode" value="4">
                                        <label>现金 </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-3 col-sm-3 control-label">付款单位:</label>
                            <div class="col-lg-9">
                                <input type="text" class="form-control" id="inputPaymentName" name="inputPaymentName"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-3 col-sm-3 control-label">收款单位(人):</label>
                            <div class="col-lg-9">
                                <input type="text" class="form-control" id="inputReceivableName"
                                       name="inputReceivableName"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-3 col-sm-3 control-label">收款银行:</label>
                            <div class="col-lg-9">
                                <input type="text" class="form-control" id="inputReceivableBank"
                                       name="inputReceivableBank"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-3 col-sm-3 control-label">收款账号:</label>
                            <div class="col-lg-9">
                                <input type="text" class="form-control" id="inputReceivableAccount"
                                       name="inputReceivableAccount"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label col-lg-3">备注:</label>
                            <div class="col-lg-9">
                                <textarea rows="4" class="form-control" id="inputRemark" name="inputRemark"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-3 col-sm-3 control-label">附件:</label>
                            <div class="form-inline col-lg-9">
                                <div class="col-lg-9">
                                <span class="input-group-btn">
                                                <button class="btn btn-primary" type="button"
                                                        onclick="upload_file(this)">上传附件</button>
                                              </span>

                                </div>
                            </div>
                        </div>
                    </div>
                </form>
                <iframe id="upload_exec_target" name="upload_exec_target" style="display: none;"></iframe>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal">关闭</button>
                <button class="btn btn-primary" id="btnAddRechargeOk">确定</button>
            </div>
        </div>
    </div>
</div>

<!--提示对话框-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="hintMessageModal" class="modal fade">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">提示</h4>
            </div>
            <div class="modal-body" id="hint_message" align="center">
            </div>
        </div>
    </div>
</div>

<script type="application/javascript">
    function upload_file(obj) {
        var t = new Date().getTime();
        var html = ' <div class="col-lg-9" style="margin-top: 10px"> <input name="file[]" id=' + t + ' type="file"> </div>';
        $(obj).parent().parent().parent().append(html);
        $("#" + t).click();
    }
    (function () {
        $('#btnAddRechargeOk').click(function () {
            var data = {};
            data.institution_code = $("#modaledit_submit_form").find('#inputInstitution').val();
            data.amount = $("#modaledit_submit_form").find('#inputRechargeAmount').val();
            data.mode = $("#modaledit_submit_form").find("input[name='recharge_mode'][checked]").val();
            data.payment_name = $("#modaledit_submit_form").find('#inputPaymentName').val();
            data.receivable_name = $("#modaledit_submit_form").find('#inputReceivableName').val();
            data.receivable_bank = $("#modaledit_submit_form").find('#inputReceivableBank').val();
            data.receivable_account = $("#modaledit_submit_form").find('#inputReceivableAccount').val();
            data.remark = $("#modaledit_submit_form").find('#inputRemark').val();

            if (data.institution_code == '' || data.institution_code == null) {
                $("#hint_message").text('机构不能为空！');
                $("#hintMessageModal").modal('show');
                return false;
            }

            if (data.amount == '' || data.amount == null) {
                $("#hint_message").text('充值金额不能为空！');
                $("#hintMessageModal").modal('show');
                return false;
            }

            if (data.payment_name == '' || data.payment_name == null) {
                $("#hint_message").text('付款单位不能为空！');
                $("#hintMessageModal").modal('show');
                return false;
            }

            if (data.receivable_name == '' || data.receivable_name == null) {
                $("#hint_message").text('收款单位不能为空！');
                $("#hintMessageModal").modal('show');
                return false;
            }

            //Form提交
            loading.show();
            $("#modaledit_submit_form").submit();
            $("#upload_exec_target").load(function () {
                try {
                    var result = eval('(' + ($("#upload_exec_target").contents().find("body").html()) + ')');
                    if (result.status == 0) {
                        window.location.reload();
                    } else {
                        var re = result.message;
                        $("#hint_message").html(re);
                        loading.hide();
                    }
                } catch (err) {
                    var re = "系统异常，请重试";
                    loading.hide();
                    $("#hint_message").html(re);
                }
//                loading.hide();
            });

//            requestJson('add-recharge-handler', data, true, function (responseData) {
//                $('#addModal').modal('hide');
//                if (responseData.status == 0) {
//                    window.location.reload();
//                } else {
//                    alert(responseData.message);
//                }
//            });
        });

        $('#detailModal').on('show.bs.modal', function (e) {
            $("#detail_content").html('正在加载数据....');
            var rechargeId = e.relatedTarget.attributes['data-recharge-id'].value;
            requestHtml('recharge-detail-partview', {recharge_id: rechargeId}, true, function (responseHtml) {
                $("#detail_content").html(responseHtml);
            });
        });

        $('#btnSearch').click(function () {
            var searchInstitutionCode = $('#searchInstitution').val();
            var searchCreditStatus = $('#searchRechargeStatus').val();
            var startTime = $("#startTime").val();
            var endTime = $("#endTime").val();
            var url = window.location.href;
            if (searchInstitutionCode) {
                url = setUrlParam(url, 'institution_code', searchInstitutionCode);
            } else {
                url = removeUrlParam(url, 'institution_code');
            }
            url = setUrlParam(url, 'recharge_status', searchCreditStatus);
            url = setUrlParam(url, 'startTime', startTime);
            url = setUrlParam(url, 'endTime', endTime);
            url = setUrlParam(url, 'page', 1);
            window.location.href = url;
        });
    })(jQuery);

    //设置模态窗:当用户点击模态框外部时不会关闭、当初始化时不显示模态框。
    $(function () {
        $('#addModal').modal({
            backdrop: 'static', show: false
        });
    });
</script>

